#!/usr/bin/env bash

set -euo pipefail

MAX="$((MAX_CLOSURE_SIZE_IN_MB * 1024 * 1024))"
OUT_PATH="${1?Usage: $0 PATH}"

CLOSURE_SIZE="$(nix path-info --closure-size --extra-experimental-features nix-command "${OUT_PATH}" | awk '{ print $2 }')"

if [ "$CLOSURE_SIZE" -gt "$MAX" ]; then
    echo "ERROR: closure size too big!"
    echo "Max closure size $MAX"
    echo "Current closure size $CLOSURE_SIZE"
    echo "Consider changing your dependencies or increasing MAX_CLOSURE_SIZE_IN_MB in .gitlab-ci.yml"
    exit 1
fi
